package app.server.project.main.service.config;

import app.server.project.core.syndb.SynDBMgr;
import cn.hutool.core.util.ClassUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SynDbConfig {

    @Autowired
    private SynDBMgr synDBMgr;

    @PostConstruct
    public void init() {
        var entityClass = ClassUtil.scanPackage("app.server.project.main.service.db.entity");
        synDBMgr.init(entityClass);
    }

    /**
     * 分页插件，如果不配置，分页插件将不生效
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
        return interceptor;
    }
}
